﻿using System;
using System.Data;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Reflection;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using AnyOffice.Components;
using AnyOffice.DAL;
using log4net;
namespace AnyOffice.general.mytable
{
    public partial class query : System.Web.UI.UserControl
    {
        protected string strTemp;

        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
   
        private void BindTree(ref StringBuilder s, int deptId, string strTemp, SqlConnection conn)
        {
            DataTable table = new DepartmentDao().SelectChildAll(conn, deptId);
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "|　";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    s.Append("<option value=\"" + table.Rows[i]["DeptId"].ToString() + "\">" + strTemp + "|─" + table.Rows[i]["DeptName"].ToString() + "</option>");
                    this.BindTree(ref s, int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp, conn);
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = new SqlConnection(ConfigManager.ConnectionString);
                DataTable table = new DepartmentDao().SelectParentAll(conn);
                StringBuilder s = new StringBuilder();
                s.Append("<select name=\"sltDeptId__\" id=\"sltDeptId__\" class=\"SmallSelect\">");
                s.Append("<option value=\"ALL\">所有</option>");
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        s.Append("<option value=\"" + table.Rows[i]["DeptId"].ToString() + "\">|─" + table.Rows[i]["DeptName"].ToString() + "</option>");
                        this.strTemp = "";
                        this.BindTree(ref s, int.Parse(table.Rows[i]["DeptId"].ToString()), this.strTemp, conn);
                    }
                }
                s.Append("</select>");
                conn.Close();
                this.litDeptId.Text = s.ToString();
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
            }
        }

    }
}